<template>
  <div class="cart-page">
    <div class="nav-bar" style="background: #FFFFFF">
      <div class="nav-status" :style="{height:statusH + 'px'}"></div>
      <div class="nav-head flex-center" :style="{height:titleH + 'px'}">
        <div class="flex-1 flex-row-v-center" style="padding-left: 10px;">
          <img style="width: 22px;height: 22px" src="/static/assets/img/icon/icon-arrow-left.png"
               @click="onClickGoBack()"/>
        </div>
        <div class="flex-1 flex-center">
          <span style="font-size: 18px; color:#000000">购物车</span>
        </div>
        <div class="flex-1"></div>
      </div>
    </div>
    <div class="clear-10">
    </div>
    <div class="cart-container" :style="{'padding-top':titleH + statusH + 'px'}">
      <block v-if="cartList.length > 0">
        <div class="cart-item flex-row-v-center" v-for="item in cartList" :key="item.id">
          <div class="checkbox-holder flex-center" @click="checkItem(item)">
            <img src="/static/assets/img/icon-checkbox.png" v-if="item.selected"/>
            <img src="/static/assets/img/icon-checkbox-empty.png" v-else/>
          </div>
          <div class="flex-1 flex-row-v-center goods-holder">
            <img :src="item.cover_img_url" class="item-img"/>
            <div class="flex-1 flex-col-space-between cart-item-content">
              <label class="cart-name">{{item.name}}</label>
              <label class="cart-desc">{{item.goods_spec_name}}</label>
              <div class="opera-btn flex-row-v-center">
                <img src="/static/assets/img/icon/icon-sub.png" @click="subItem(item)"/>
                <label class="item-count"> {{item.amount}} </label>
                <img src="/static/assets/img/icon/icon-add.png" @click="addItem(item)"/>
              </div>
            </div>
            <div class="cart-price">￥{{item.amount * item.price / 100}}</div>
          </div>
        </div>
        <div class="cart-bottom flex-row-space-between">
          <div class="bottom-left flex-row-v-center" @click="checkAll">
            <div class="checkbox-holder flex-center">
              <img src="/static/assets/img/icon-checkbox.png" v-if="totalChecked"/>
              <img src="/static/assets/img/icon-checkbox-empty.png" v-else/>
            </div>
            <span class="all-text">全选</span>
          </div>
          <div class="bottom-right flex-row-v-center">
            <div class="confirm-btn flex-center" @click="goToConfirmPage">
              结算(￥{{totalPrice}})
            </div>
          </div>

        </div>
      </block>
      <block v-else>
        <div class="empty-div flex-col-v-center">
          <img src="/static/assets/img/empty-ticket.png" class="empty-img"/>
          <label class="empty-text">暂无商品</label>
        </div>
      </block>
    </div>
  </div>
</template>

<script>
  import global from 'global'
  import Core from 'core'
  import Common from 'common'
  // todo  列表为空
  export default {
    data () {
      return {
        statusH: global.state.statusH,
        titleH: global.state.titleH,

        cartList: [],

        totalChecked: false,
        totalPrice: 0
      }
    },

    onShow () {
      this.getCartList()
    },

    methods: {
      onClickGoBack () {
        Core.Util.goBack()
      },

      initCartList () {
      },

      updateTotalStat () {
        let totalPrice = 0
        let totalChecked = true
        this.cartList.forEach(item => {
          if (item.selected) {
            totalPrice = totalPrice + item.amount * item.price / 100
          } else {
            totalChecked = false
          }
        })
        this.totalPrice = totalPrice
        this.totalChecked = totalChecked
      },

      checkAll () {
        this.totalChecked = !this.totalChecked
        this.cartList.forEach(item => {
          item.selected = this.totalChecked
        })
        this.updateTotalStat()
      },

      checkItem (item) {
        item.selected = !item.selected
        this.updateTotalStat()
      },

      subItem (item) {
        item.amount = item.amount > 0 ? item.amount - 1 : 1
        this.updateCart(item.id, item.amount)
      },

      addItem (item) {
        item.amount += 1
        this.updateCart(item.id, item.amount)
      },

      getCartList () {
        Core.Api.Cart.cartList()
          .then(res => {
            let list = res.goods_cart_list
            list.map(item => {
              item['selected'] = false
            })
            this.cartList = list
            let listInDb = []
            list.forEach(v => {
              listInDb.push({
                goodsId: v.goods_id,
                amount: v.amount
              })
            })
            Core.Data.set('cart_list', listInDb)
          })
          .catch(err => {
            Core.Util.showTip('', err.message)
          })
      },

      updateCart (id, amount) {
        let url = ''
        if (amount > 0) {
          url = Core.Api.Cart.updateCart(id, amount)
        } else {
          url = Core.Api.Cart.deleteCart(id)
        }

        url
          .then(res => {
            this.getCartList()
          })
          .catch(err => {
            Core.Util.showTip('', err.message)
          })
      },

      goToConfirmPage () {
        let selectList = this.cartList.filter(item => {
          return item.selected
        })
        let idList = []
        selectList.forEach(item => {
          idList.push(item.id)
        })
        let ids = idList.join(',')
        if (ids) {
          Common.go(`/pages/购物车确认/main?cart_ids=${ids}`)
        }
      }

    }
  }
</script>

<style lang="scss" scoped>
  .cart-page {
    background: #f9f9f9;
    height: 100%;

    .cart-container {
      .cart-item {
        background: transparent;
        padding: 0 10px;
        margin-bottom: 10px;
        /*border-bottom: 1px solid #e5e5e5;*/
        .item-img {
          height: 82px;
          width: 82px;
          margin-right: 10px;
        }
      }
      .goods-holder {
        background: #ffffff;
        padding: 16px 10px;
        border-radius: 4px;
      }
      .cart-item-content {
        align-items: flex-start;
        .cart-name {
          font-size: 13px;
          color: #313131;
        }
        .cart-desc {
          padding: 4px 6px;
          font-size: 11px;
          line-height: 11px;
          color: #313131;
          background: #F9F9F9;
          margin-top: 4px;
        }
        .opera-btn {
          box-sizing: border-box;
          height: 22px;
          margin-top: 12px;
          img {
            width: 20px;
            height: 20px;
          }
          .item-count {
            width: 16px;
            text-align: center;
            padding: 0 10px;
            font-size: 14px;
            margin-top: 0;
          }
        }
      }
      .cart-price {
        align-self: flex-end;
        font-size: 14px;
        line-height: 14px;
        color: #F15858;
        padding-bottom: 4px;
      }
    }

    .cart-bottom {
      background-color: #ffffff;
      position: fixed;
      bottom: 0;
      width: 100%;
      height: 50px;
      padding: 4px 10px;
      .all-text {
        color: #939393;
        font-size: 14px;
        margin-left: -4px;
      }
      .confirm-btn {
        width: 175px;
        height: 40px;
        background: linear-gradient(to right, rgb(252, 63, 7), rgb(252, 123, 10));
        border-radius: 40px;
        font-size: 16px;
        color: #ffffff;
      }
    }

    .bg-white {
      background: white;
    }

    .empty-div {
      margin-top: 30%;
      .empty-img {
        width: 167px;
        height: 138px;
        margin-bottom: 20px;
      }
      .empty-text {
        margin-bottom: 20px;
        color: #939393;
        font-size: 16px;
      }
    }

    .checkbox-holder {
      img {
        width: 20px;
        height: 20px;
      }
      margin-right: 14px;
      margin-left: 4px;
    }
  }
</style>
